package com.udi.project.business.local.di.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.udi.common.utils.security.ShiroUtils;
import com.udi.framework.web.controller.BaseController;
import com.udi.framework.web.domain.AjaxResult;
import com.udi.framework.web.page.TableDataInfo;
import com.udi.project.business.base.domain.BaseCorpVo;
import com.udi.project.business.base.service.BaseCorpService;
import com.udi.project.business.local.di.domain.DtDi;
import com.udi.project.business.local.di.domain.NameCode;
import com.udi.project.business.local.di.service.DiGenerateService;
import com.udi.project.business.upload.diup.domain.Product;
import com.udi.project.business.local.di.service.DiMangeService;

import com.udi.project.business.pojo.damain.*;
import com.udi.project.business.pojo.damain.Package;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Admin
 * Di管理控制器
 */
@Controller
@RequestMapping("/dimange")
@Slf4j
public class DiMangeController extends BaseController {

    private String pre = "local/dimana";

    @Autowired
    private DiMangeService diMangeService;

    @RequiresPermissions("dimange:mana:view")
    @GetMapping("/mana")
    public String dimain1(){
        return pre+"/dimana";
    }

    @RequiresPermissions("dimange:ls:view")
    @GetMapping("/ls")
    public String dimain2(){
        return pre+"/dils";
    }

    @GetMapping("/add")
    public String diadd(){
        return pre+"/add";
    }

    @GetMapping("/edit")
    public String diedit(){
        return pre+"/edit";
    }

    @GetMapping("/detail/{Id}")
    public String detail(@PathVariable("Id") Integer id, ModelMap mmap){

        mmap.put("di_detail", diMangeService.selectDiById(id));
        return pre + "/detail";
    }

    @GetMapping("/detaills/{Id}")
    public String detaills(@PathVariable("Id") Integer id, ModelMap mmap){
        mmap.put("di_detail", diMangeService.selectDilsById(id));
        return pre + "/detaills";
    }
    /**
     * @Description:查询DI列表
     * @param ids di码
     * @return: TableDataInfo
     * @Date: 2021/8/9 11:15
     * http://localhost:8080/dimange/se?code=1&time=2021
     */
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo getDiList(DtDi ids){
        //开启分页，待数据查询时进行sql组装
        startPage();
        //获取当前用户数据
        JSONObject user_arr = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String current_dept = ((JSONObject)user_arr.get("dept")).getString("deptName");
        String deptId = ((JSONObject)user_arr.get("dept")).getString("deptId");
        //获取权限标识符
        String roleKey =((JSONObject)((JSONArray)user_arr.get("roles")).get(0)).getString("roleKey");
        //根据普通用户和管理员查询不同权限数据,查询得出是否包含字符串“admin”
        if(roleKey.contains("admin")){
            return getDataTable(diMangeService.selectDi(ids));
        }else{
            ids.setDeptName(current_dept);
            ids.setDeptId(deptId);
            return getDataTable(diMangeService.selectDi(ids));
        }
    }
    /**
     * 历史信息，已导出
     * @param ids
     [ids]* @return com.udi.framework.web.page.TableDataInfo
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/13 17:16
     */

    @RequestMapping("/listls")
    @ResponseBody
    public TableDataInfo getDiListls(DtDi ids){
        //开启分页，待数据查询时进行sql组装
        startPage();
        //获取当前用户数据
        JSONObject user_arr = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String current_dept = ((JSONObject)user_arr.get("dept")).getString("deptName");
        String deptId = ((JSONObject)user_arr.get("dept")).getString("deptId");
        //获取权限标识符
        String roleKey =((JSONObject)((JSONArray)user_arr.get("roles")).get(0)).getString("roleKey");
        List<DtDi> list;
        //根据普通用户和管理员查询不同权限数据,查询得出是否包含字符串“admin”
        if(roleKey.contains("admin")){
            return getDataTable(diMangeService.selectDils(ids));
        }else{
            ids.setDeptName(current_dept);
            ids.setDeptId(deptId);
            return getDataTable(diMangeService.selectDils(ids));
        }
    }

    /**
     * @Description:清空DI，删除DI
     * @param DtDi 传入主键ID DtDi
     * @return: int
     * @Date: 2021/8/9 17:15
     */
    /**http://localhost:8080/dimange/de*/
    @RequestMapping("/del")
    @ResponseBody
    public AjaxResult deltDi(String ids) throws Exception {
        return toAjax(diMangeService.delectDi(ids));
    }
    

    

    @PostMapping("/seInfor")
    @ResponseBody
    public String selectInfor(String di){
        return diMangeService.selectInformation(di).toString();
    }



    @PostMapping("/upInfor")
    @ResponseBody
    public AjaxResult  upInfor(List<Product> identity){
        return diMangeService.updateInformation(identity);
    }


    @PostMapping("/deInfor")
    @ResponseBody
    public AjaxResult  deInfor(String di_id){
        return diMangeService.delectInformation(di_id);
    }


    @PostMapping("/inInfor")
    @ResponseBody
    public AjaxResult inInfor(List<Product> identity){
        return diMangeService.insertInformation(identity);
    }



    /**-------------包装标识CRUD*/

    @PostMapping("/sepkgdi")
    @ResponseBody
    public List<Package> sepkgDi(List<String> id){
        return diMangeService.selectpkgDi(id);
    }

    @PostMapping("/sepkg")
    @ResponseBody
    public List<Package> sepkg(String id){
        return diMangeService.selectpkg(id);
    }


    @PostMapping("/inPkg")
    @ResponseBody
    public String inPkg(List<Package> packag){
        return diMangeService.insertPkg(packag);
    }


    @PostMapping("/dePkg")
    @ResponseBody
    public String dePkg(List<Package> packag){
        return diMangeService.deletePkg(packag);
    }

    @PostMapping("/upPkg")
    @ResponseBody
    public String upPkg(List<Package> packag){
        return diMangeService.updatePkg(packag);
    }





    /**-------------存储或操作信息CRUD*/
    @PostMapping("/sestoragedi")
    @ResponseBody
    public List<Storage> seStorageDi(List<String> id){
        return diMangeService.selectStorageDi(id);
    }


    @PostMapping("/sestorage")
    @ResponseBody
    public List<Storage> seStorage(String id){
        return diMangeService.selectStorage(id);
    }


    @PostMapping("/instorage")
    @ResponseBody
    public String inStorage(List<Storage> storage){
        return diMangeService.insertStorage(storage);
    }

    @PostMapping("/uptorage")
    @ResponseBody
    public String upStorage(List<Storage> storage){
        return diMangeService.updateStorage(storage);
    }

    @PostMapping("/detorage")
    @ResponseBody
    public String deStorage(List<Storage> id){
        return diMangeService.deleteStorage(id);
    }




    /**-------------临床尺寸信息CRUD*/
    @PostMapping("/seclinicaldi")
    @ResponseBody
    public List<Clinical> seClinicalDi(List<String> id){
        return diMangeService.selectClinicalDi(id);
    }

    @PostMapping("/seclinical")
    @ResponseBody
    public List<Clinical> seClinical(String id){
        return diMangeService.selectClinical(id);
    }

    @PostMapping("/inclinical")
    @ResponseBody
    public String inClinical(List<Clinical> clinical){
        return diMangeService.insertClinical(clinical);
    }


    @PostMapping("/upclinical")
    @ResponseBody
    public String upClinical(List<Clinical> clinical){
        return diMangeService.updateClinical(clinical);
    }

    @PostMapping("/declinical")
    @ResponseBody
    public String deClinical(List<Clinical> clinical){
        return diMangeService.deleteClinical(clinical);
    }


    /**
     *
     * @Description: 返回所有部门名称与部门编号,以及与之对应的厂商码【JSON数组】
     * @return: java.lang.String
     * @Date: 2021/8/14 16:48
     */
    @RequestMapping("/getDeptMes")
    @ResponseBody
    public JSONArray seDept(){
        JSONObject current = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String currdeptId = (String)((JSONObject)current.get("dept")).getString("deptId");
        String roleKey =((JSONObject)((JSONArray)current.get("roles")).get(0)).getString("roleKey");
        JSONArray array;
        if(roleKey.contains("admin")){
            List<BaseCorpVo> list = diMangeService.getdept("");
            return JSONArray.parseArray(JSON.toJSONString(list));
        }else{
            List<BaseCorpVo> list = diMangeService.getdept(currdeptId);
            return JSONArray.parseArray(JSON.toJSONString(list));
        }
    }

    /**
     * 医疗器械唯一标识信息系统数据导入模板下载
     * @param
     []* @return void
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/13 14:51
     */
    @RequestMapping("/importTemp")
    @ResponseBody
    public AjaxResult changeStatu(){
        //根据路径进行修改
        return AjaxResult.success("医疗器械唯一标识信息系统数据导入模板.xls");
    }

    /**
     *
     * @Description: 返回所有包装指示符信息【JSON数组】
     * @return: java.lang.String
     * @Date: 2021/8/14 16:48
     */
    @RequestMapping("/selectpkg")
    @ResponseBody
    public JSONArray selpkg(){
        List<dtPkg> list = diMangeService.selectPkg();
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
        return array;
    }
}
